<?php

namespace Biz\service;

use Biz\cache\Stock as cStock;
use Biz\model\GoodsStockLog;
use Biz\module\Behaviors\StockBehavior;
use Doraemon\tools\Log;
use Biz\pockets\constInterface\ActivityInfo;
/**
 * @deprecated
 */
class Stock
{


    const TYPELOGMAP = [
        GoodsStockLog::TYPE_NORMAL_GOODS => ActivityInfo::ACTIVITY_TYPE_NORMAL,
        GoodsStockLog::TYPE_LIMIT_TIME_ROBBING => ActivityInfo::ACTIVITY_TYPE_LIMIT_TIME_ROB
    ];

    /**
     * @param $order_no string 父订单ID
     * @param $stock_params array
     *
     *   [
     *  "order_no1" => [
     *     ["sku_id" => 1, "goods_id" => 1, "buy_num" =>3, "promo_type" => 1, "promo_id" => 1],
     *     ["sku_id" => 2,  "goods_id" => 1,"buy_num" =>5, "promo_type" => 0, "promo_id" => 0],
     *   ],
     *  "order_no2" => [
     *     ["sku_id" => 1, "goods_id" => 1, "buy_num" =>3, "promo_type" => 0, "promo_id" => 0],
     *     ["sku_id" => 2, "goods_id" => 1, "buy_num" =>5, "promo_type" => 1, "promo_id" => 3],
     *   ]
     * ]
     *
     * @return boolean
     */
    public static function DecrStockByOrderNo($pOrderNo, $buyNums)
    {
        //防止订单重复提交 枷锁
        if ($buyNums && cStock::dataLock($pOrderNo)) {

        	Log::debug($pOrderNo, $buyNums);

            $order_nos = array_keys($buyNums);
            $infos = GoodsStockLog::getByForeignId($order_nos);
            if ($infos) {
                Log::Notice('存在重复提交的订单 ' . json_encode($order_nos, JSON_UNESCAPED_UNICODE));
                throw new \Exception('重复提交订单');
            }


            $allBuyNums = [];
            foreach ($buyNums as $order_no => $buyNum) {
                foreach ($buyNum as $buyInfo) {
                    $buyInfo['order_no'] = $order_no;
                    $buyInfo['p_order_no'] = $pOrderNo;
                    $allBuyNums[] = $buyInfo;
                }
            }

            Log::debug('扣减库存', compact('allBuyNums'));
            $result = StockBehavior::DecrStocksCounter($allBuyNums);
            if ($result === false) {
            	StockBehavior::setStocksCache(array_column($allBuyNums, 'goods_id'));
            	$result = StockBehavior::DecrStocksCounter($allBuyNums);
			}

			return $result;

        }

    }


    /**
     * @param $order_nos
     *
     * @return bool
     *
     */
    public static function IncrStockByOrderNos($order_nos)
    {
        if ($order_nos && cStock::dataLock(md5(json_encode($order_nos)))) {

			Log::debug('回滚订单',$order_nos);

			$logInfos = GoodsStockLog::getByForeignId($order_nos);
            if ($logInfos) {
                $buyNums = array_map(function ($logInfo) {

                    $logInfo['order_no'] = $logInfo['foreign_id'];

//                    $logInfo['promo_type'] = self::TYPELOGMAP[$logInfo['type']];

                    $logInfo['buy_num'] = abs($logInfo['change_num']);

                    unset($logInfo['id']);
                    unset($logInfo['type']);
                    unset($logInfo['add_time']);
                    unset($logInfo['target_num']);
                    unset($logInfo['change_num']);
                    unset($logInfo['action_uid']);
                    unset($logInfo['foreign_id']);
                    return $logInfo;
                }, $logInfos);


                return StockBehavior::IncrStocksCounter($buyNums);

            } else {
                Log::Notice('不在的订单列表 ' . json_encode($order_nos, JSON_UNESCAPED_UNICODE));
                return false;
            }

        }
    }


    public static function getStocksByGoodsIds($goodsIds){
        list($cacheData, $noCacheKeys)=cStock::pHGetAllItems($goodsIds);
        if($noCacheKeys){
            self::setStockCache($noCacheKeys);
            list($cacheData, $noCacheKeys)=cStock::pHGetAllItems($goodsIds);
        }
        return $cacheData;
    }

    public static function getSotcksByGoodsIds($goodsIds){
        list($cacheData, $noCacheKeys)=cStock::pHGetAllItems($goodsIds);
        if($noCacheKeys){
            self::setStockCache($noCacheKeys);
            list($cacheData, $noCacheKeys)=cStock::pHGetAllItems($goodsIds);
        }
        return $cacheData;
    }


    public static function setStockCache($goods_id)
    {
        StockBehavior::setStocksCache((array)$goods_id);
        return true;
    }


}